﻿

using OpenCvSharp;

using Xc.DataAccess.CodeReader.Properties;
using Xc.DataAccess.Core;

namespace Xc.DataAccess.CodeReader.Models.Filters
{
    /// <summary>
    /// 查找轮廓
    /// </summary>
    [Filter("查找轮廓", FilterType.Algorithm)]
    public class AlgorithmFindContours : FilterBase
    {
        /// <summary>
                  /// 查找模式
                  /// </summary>
        [InputParameter("查找模式")]
        public RetrievalModes RetrievalModes { get; set; }
        /// <summary>
        /// 逼近方案
        /// </summary>
        [InputParameter("逼近方案")]
        public ContourApproximationModes ContourApproximationModes { get; set; }
        /// <summary>
        /// 偏移量
        /// </summary>
        [InputParameter("偏移量")]
        public CVPoint Offset { get; set; } = new CVPoint();
        /// <summary>
        /// 绘制轮廓
        /// </summary>
        [InputParameter("绘制轮廓")]
        public bool UseDraw { get; set; }
        /// <summary>
        /// 颜色
        /// </summary>
        [InputParameter("颜色")]
        public Scalar Color { get; set; }
        /// <summary>
        /// 粗细
        /// </summary>
        [InputParameter("粗细")]
        public int Thickness { get; set; }
        /// <summary>
        /// 线条类型
        /// </summary>
        [InputParameter("线条类型")]
        public LineTypes LineTypes { get; set; }
        internal override ResultData<Mat> UseChannel(Mat mat, FilterBase[] filterInfos, FilterBase preFilterBase)
        {
            Point[][] points = new Point[][] { };
            HierarchyIndex[] hierarchyIndices = new HierarchyIndex[] { };
            Cv2.FindContours(mat, out points, out hierarchyIndices, RetrievalModes, ContourApproximationModes, Offset);
            if (UseDraw)
            {
                for (int i = 0; i < hierarchyIndices.Length; i++)
                {
                    Cv2.DrawContours(mat, points,i,Color,Thickness,LineTypes, hierarchyIndices);
                }


            }
            return ResultData<Mat>.Accept(TextResource.find_complete, mat);
        }
    }
}
